package com.example.three.Controller.ShopUserController;


import com.example.three.DO.SysUser;
import com.example.three.DO.TableData;
import com.example.three.Service.SysUserService.UserService;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller

public class ShopUserController {
    @Resource
    private UserService UserService;
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session,Model model) {
        System.out.println(username);
        System.out.println(password);
        SysUser loginUser = UserService.login(username,password);
        session.setAttribute("shopid",username);
        //model前端接受，session后端调用
        model.addAttribute("user",loginUser);
        //自动生成id
       /* ArrayList<SysUser> Userlist = UserService.findAll();*/
        /*for(int i=0;i<Userlist.size();i++){
            System.out.println(Userlist.get(i).getUserid());
        }*/
        if(loginUser!=null){
            return  "index11";
        }
        else {
            //此处需要加用户名或密码错误提示
            model.addAttribute("errorinfo","用户名或者密码错误");
            return "Login2";
        }
    }

    @RequestMapping("/resigened")
    public String adduser(String userid,long poseid,String username,String password,String usertype,String email,
                          String phonenumber,String sex,String status){
//        HttpSession session = null;
//        session.setAttribute("shopid",userid);

        UserService.addBy(userid,poseid,username,password,usertype,email,phonenumber,sex,status);
        return "Login2";
    }

    @RequestMapping("/To-recom")

    public String ToRecom(Model model, HttpSession session){
        SysUser loginUser = UserService.findBy(String.valueOf(session.getAttribute("shopid")));
        model.addAttribute("user",loginUser);
        System.out.println(String.valueOf(session.getAttribute("shopid")));
        System.out.println(model.getAttribute("user"));
        return "recom";
    }

    @RequestMapping("/mylist")
    @ResponseBody
    public TableData mylist(HttpSession session, Model model){
        String shopid = String.valueOf(session.getAttribute("shopid"));
        ArrayList<SysUser> mylist = new ArrayList<>();
        SysUser sysUser = UserService.findBy(shopid);
        mylist.add(sysUser);
        model.addAttribute("mylist",mylist);//model传值给前端
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setData(mylist);
        return tableData;
    }

    @RequestMapping("/updatepassword")
    public String updatepassword(){
        return "updatepassword";
    }

    @RequestMapping("/updatepassword1")
    public String updatepassword1(String password,HttpSession session){
        String shopid = String.valueOf(session.getAttribute("shopid"));
        UserService.updatepassword(shopid,password);
        return "updatepassword";
    }
    @RequestMapping("/updatainformation")
    public void updatainformation(HttpSession session,String username,String phonenumber,String email,String sex,Model model){
        SysUser loginUser = UserService.findBy(String.valueOf(session.getAttribute("shopid")));
        model.addAttribute("user",loginUser);
        String shopid = String.valueOf(session.getAttribute("shopid"));
        UserService.updateinformation(shopid,username,phonenumber,email,sex);
    }
    @RequestMapping("/index1")
    public String index(){
        return "index";
    }
}

